package com.thghh.fsbrowser.fs;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.reflections.Reflections;

import com.thghh.fsbrowser.annotation.FileSystemType;

/**
 * com.thghh.fsbrowser.fs.DefaultFileSystem
 * 
 * @author pengzhikang
 * @date 2018年9月25日上午1:22:03
 * @since 1.0
 */
class DefaultFileSystem {

	private final static Map<Class<? extends IFileSystem>, IFileSystem> fsMap = new ConcurrentHashMap<>();

	static {
		try {
			Reflections reflections = new Reflections("com.thghh.fsbrowser.fs");
			Set<Class<? extends IFileSystem>> subTypes = reflections.getSubTypesOf(IFileSystem.class);
			for (Class<? extends IFileSystem> clazz : subTypes) {
				if (!Modifier.isAbstract(clazz.getModifiers())) {
					FileSystemType fsType = clazz.getAnnotation(FileSystemType.class);
					if (fsType != null) {
						fsMap.put(clazz, clazz.getDeclaredConstructor().newInstance());
					}
				}
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
	}

	public static IFileSystem getFileSystem() {
		return new LocalFileSystem();
	}


}
